package com.vcaresolution.logistic.controller;

import java.util.Enumeration;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.vcaresolution.logistic.form.LoginForm;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>LogoutController</code> is used to track all login related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
@RequestMapping(value=ApplicationConstants.LOGOUT_URL)
public class LogoutController extends BaseController{

	private LoginForm loginForm;
	
	@Autowired
	public void setLoginForm(LoginForm loginForm) {
		this.loginForm = loginForm;
	}
	
	/**
	 * This method is called when the GET request has been made to the controller
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView logout(HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		
		//Remove all session attributes - This step is actually not required but to be double sure, we are removing all attributes
		//Then invalidating the session
		Enumeration<String> sessionAttributes = session.getAttributeNames();
		while(sessionAttributes.hasMoreElements()){
			session.removeAttribute(sessionAttributes.nextElement());
		}
		
		//clear the session
		session.invalidate();
		
		modelAndView.addObject(ApplicationConstants.LOGINFORM_MODELATTRIBUTE, this.loginForm);
		modelAndView.setViewName(ApplicationConstants.LOGIN_URL);
		return modelAndView;
	}
	
	
}